package photobox.manage

import grails.converters.JSON
import photobox.BaseController
import photobox.exception.NotFoundException
import photobox.photo.PhotoFrame
import photobox.utils.ParamUtils
import photobox.utils.ResultUtil

class MngPhotoFrameController extends BaseController {

    def photoFrameService
    def index() {}

    def list() {
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id", "name", "description"], [sort: 'id', order: "desc"])

        def frameRs = photoFrameService.listPhotoFrame(searchParams, pageParams, sortParams)
        render ResultUtil.pageResults(frameRs.rs, frameRs.total, pageParams,
                [id: 'id', name: 'name', description: 'description', isDeal: 'isDeal']) as JSON
    }
    def create(){

    }
    def save(){
        photoFrameService.createFrame(params)
        render([error: 0, message: "创建新相框完成"] as JSON)
    }
    def edit() {
        def photoFrame = PhotoFrame.get(params.id)
        if (!photoFrame) {
            throw new NotFoundException("没有找到对应的相框")
        }
        return [photoFrame: photoFrame]
    }

    def update() {
        photoFrameService.updateFrame(params)
        render([error: 0, message: "更新相框信息完成"] as JSON)
    }

    def delete() {
        photoFrameService.deleteFrame(params)
        render([error:0, message: "删除相框完成"] as JSON)
    }
}
